package org.edu.modules.jbsz.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jbsz.entity.VeJwBzr;
import org.edu.modules.jbsz.entity.VeJwKcxz;
import org.edu.modules.jbsz.service.IVeJwBzrService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: ve_jw_bzr
 * @Author: jeecg-boot
 * @Date: 2021-06-13
 * @Version: V1.0
 */
@Api(tags = "ve_jw_bzr")
@RestController
@RequestMapping("/jxgz/veJwBzr")
@Slf4j
public class VeJwBzrController extends BaseController<VeJwBzr, IVeJwBzrService> {
    @Autowired
    private IVeJwBzrService veJwBzrService;

    /**
     * 分页列表查询
     *
     * @param veJwBzr
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "班主任-分页列表查询")
    @ApiOperation(value = "班主任-分页列表查询", notes = "班主任-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwBzr veJwBzr,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJwBzr> queryWrapper = QueryGenerator.initQueryWrapper(veJwBzr, req.getParameterMap());
        queryWrapper.eq("del_flag", 0);
        Page<VeJwBzr> page = new Page<VeJwBzr>(pageNo, pageSize);
        IPage<VeJwBzr> pageList = veJwBzrService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 回收站分页列表查询
     *
     * @param veJwBzr
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "班主任-回收站分页列表查询")
    @ApiOperation(value = "班主任-回收站分页列表查询", notes = "班主任-回收站分页列表查询")
    @GetMapping(value = "/rlist")
    public Result<?> queryPageRList(VeJwBzr veJwBzr,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<VeJwBzr> queryWrapper = QueryGenerator.initQueryWrapper(veJwBzr, req.getParameterMap());
        queryWrapper.eq("del_flag", 1);
        Page<VeJwBzr> page = new Page<VeJwBzr>(pageNo, pageSize);
        IPage<VeJwBzr> pageList = veJwBzrService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 添加
     *
     * @param veJwBzr
     * @return
     */
    @AutoLog(value = "ve_jw_bzr-添加")
    @ApiOperation(value = "ve_jw_bzr-添加", notes = "ve_jw_bzr-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwBzr veJwBzr) {
        veJwBzrService.save(veJwBzr);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwBzr
     * @return
     */
    @AutoLog(value = "ve_jw_bzr-编辑")
    @ApiOperation(value = "ve_jw_bzr-编辑", notes = "ve_jw_bzr-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwBzr veJwBzr) {
        veJwBzrService.updateById(veJwBzr);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "班主任-通过id删除")
    @ApiOperation(value = "班主任-通过id删除", notes = "班主任-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        if (veJwBzrService.getById(id) != null) {
            veJwBzrService.updateById(veJwBzrService.getById(id).setDelFlag(1));
            return Result.ok("删除成功!");
        }
        return Result.error("参数错误！");
    }

    /**
     * 通过id销毁
     *
     * @param id
     * @return
     */
    @AutoLog(value = "班主任-通过id销毁")
    @ApiOperation(value = "班主任-通过id销毁", notes = "班主任-通过id销毁")
    @DeleteMapping(value = "/destroy")
    public Result<?> destroy(@RequestParam(name = "id", required = true) String id) {
        veJwBzrService.removeById(id);
        return Result.ok("销毁成功!");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ve_jw_bzr-批量删除")
    @ApiOperation(value = "ve_jw_bzr-批量删除", notes = "ve_jw_bzr-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        VeJwBzr bzr = null;
        for (String id : list) {
            bzr = veJwBzrService.getById(id);
            if (bzr != null) {
                veJwBzrService.updateById(bzr.setDelFlag(1));
            }
        }
        return Result.ok("批量删除成功!");
    }


    /**
     * 批量销毁
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "班主任-批量销毁")
    @ApiOperation(value = "班主任-批量销毁", notes = "班主任-批量销毁")
    @DeleteMapping(value = "/destroyBatch")
    public Result<?> destroyBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwBzrService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量销毁成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ve_jw_bzr-通过id查询")
    @ApiOperation(value = "ve_jw_bzr-通过id查询", notes = "ve_jw_bzr-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwBzr veJwBzr = veJwBzrService.getById(id);
        if (veJwBzr == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwBzr);
    }


    /**
     * 通过id还原
     *
     * @param id
     * @return
     */
    @AutoLog(value = "课程性质-通过id还原")
    @ApiOperation(value = "课程性质-通过id还原", notes = "课程性质-通过id还原")
    @GetMapping(value = "/restore")
    public Result<?> restore(@RequestParam(name = "id", required = true) String id) {
        if (veJwBzrService.getById(id) != null) {
            veJwBzrService.updateById(veJwBzrService.getById(id).setDelFlag(0));
            return Result.ok("还原成功!");
        }
        return Result.error("参数错误！");

    }

    /**
     * 批量还原
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "课程性质-批量还原")
    @ApiOperation(value = "课程性质-批量还原", notes = "课程性质-批量还原")
    @GetMapping(value = "/restoreBatch")
    public Result<?> restoreBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        VeJwBzr bzr = null;
        for (String id : list) {
            bzr = veJwBzrService.getById(id);
            if (bzr != null) {
                veJwBzrService.updateById(bzr.setDelFlag(0));
            } else {
                return Result.error("参数错误！");
            }
        }
        return Result.ok("批量还原成功!");

    }


    /**
     * 导出excel
     *
     * @param request
     * @param veJwBzr
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwBzr veJwBzr) {
        return super.exportXls(request, veJwBzr, VeJwBzr.class, "ve_jw_bzr");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwBzr.class);
    }

}
